package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.MdUnitMeasure;
import com.qf.service.MasterDataService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * --- Be Humble and Hungry ---
 *
 * @author McMeloxD
 * @date 2024/7/16
 * @desc 主数据接口
 */
@RestController
@RequestMapping("/md/unitMeasure")
public class MasterDataController {
    @Autowired
    private MasterDataService mdService;

    /**
     * 获取所有计量单位
     *
     * @param
     * @return list
     */
    @GetMapping("/getAllUM")
    public R getAllUnitMeasure(@RequestParam HashMap<String, String> map) {
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        String measureCode = map.get("measureCode");
        String measureName = map.get("measureName");
        // 使用步骤
        // 1 先设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        // 2 正常执行查询
        List<MdUnitMeasure> list = mdService.getAllUnitMeasure(measureCode, measureName);
        // 3 通过查询返回的list创建出分页信息,PageInfo内包含所有分页数据,可以点入源码查看
        PageInfo<MdUnitMeasure> info = new PageInfo<>(list);
        return R.ok(info);
    }

    /**
     * 根据单位编码删除一条数据
     *
     * @param measureCode
     * @return n
     */
    @GetMapping("/delUM")
    public R delUnitMeasure(String measureCode) {
        int n = mdService.delUnitMeasure(measureCode);
        return R.ok();
    }

    /**
     * 根据多个单位编码删除多条数据
     *
     * @param measureCodes
     * @return n
     */
    @GetMapping("/delUMBatch")
    public R delUMBatch(@RequestParam List<String> measureCodes) {
        int n = mdService.delBatchUM(measureCodes);
        return R.ok();
    }

    /**
     * 增加一条记录单位数据
     *
     * @param mdUnitMeasure
     * @return n
     */
    @PostMapping("/addUMBatch")
    public R addUMBatch(@RequestBody MdUnitMeasure mdUnitMeasure) {
        int n = mdService.addUMBatch(mdUnitMeasure);
        return R.ok();
    }

    /**
     * 更改一条记录单位数据
     *
     * @param mdUnitMeasure
     * @return n
     */
    @PostMapping("/editUMBatch")
    public R editUMBatch(@RequestBody MdUnitMeasure mdUnitMeasure) {
        int n = mdService.editUMBatch(mdUnitMeasure);
        return R.ok();
    }


}
